<?php if(!isset($_asset_values)) {
    $_asset_values = [];
}
$_asset_values = array_merge([
    'title' => '',
    'category_id' => '0',
    'godot_version' => '',
    'version_string' => '',
    'download_provider' => '',
    'download_commit' => '',
    'browse_url' => '',
    'issues_url' => '',
    'icon_url' => '',
    'description' => '',
    'cost' => '',
    'previews' => [],
], $_asset_values);
?>

<div class="form-group">
        <label class="col-md-4 control-label required_mark" for="title">Asset Name</label>
        <div class="col-md-5">
                <input id="title" name="title" type="text" placeholder="One-button game creation tool" class="form-control input-md" required="" value="<?php echo esc($_asset_values['title']) ?>">
                <!-- <span class="help-block">The name of the asset.</span> -->
        </div>
</div>

<div class="form-group">
    <label class="col-md-4 control-label required_mark" for="description">Description</label>
    <div class="col-md-5">
        <textarea class="form-control" id="description" name="description" placeholder="This tool allows you to make games with the click of a button.&#10;It supports..." required="" rows="7"><?php echo esc($_asset_values['description']) ?></textarea>
        <!-- <span class="help-block">The description which would be visible on the asset page.</span> -->
    </div>
</div>

<div class="form-group">
    <label class="col-md-4 control-label required_mark" for="category">Category</label>
    <div class="col-md-5">
        <select id="category" name="category_id" class="form-control" required="">
            <?php if(!$_asset_values['category_id']) { ?>
                <option value="" selected="">
                    Select...
                </option>
            <?php } ?>
            <?php foreach($constants['category_type'] as $type => $type_name) if(is_int($type)) { ?>
                <optgroup label="<?php echo esc(ucfirst($type_name . 's')) ?>">
                    <?php foreach($categories as $key => $category) if($category['type'] == $type) { ?>
                        <option value="<?php echo esc($category['id']) ?>" <?php if($category['id'] == $_asset_values['category_id']) echo 'selected=""'; ?>>
                            <?php echo esc($category['name']) ?>
                        </option>
                    <?php } ?>
                </optgroup>
            <?php } ?>
        </select>
        <!-- <span class="help-block">The category the asset belongs to.</span> -->
    </div>
</div>

<div class="form-group">
    <label class="col-md-4 control-label required_mark" for="license">License</label>
    <div class="col-md-5">
        <select id="license" name="cost" class="form-control" required="">
            <?php $licenses = [
                'MIT' => 'MIT',
                'MPL-2.0' => 'MPL-2.0',
                'GPLv3' => 'GPL v3',
                'GPLv2' => 'GPL v2',
                'LGPLv3' => 'LGPL v3',
                'LGPLv2.1' => 'LGPL v2.1',
                'LGPLv2' => 'LGPL v2',
                'AGPLv3' => 'AGPL v3',
                'Apache-2.0' => 'Apache 2.0',
                'CC0' => 'CC0 1.0 Universal',
                'CC-BY-4.0' => 'CC BY 4.0 International',
                'CC-BY-3.0' => 'CC BY 3.0 Unported',
                'CC-BY-SA-4.0' => 'CC BY-SA 4.0 International',
                'CC-BY-SA-3.0' => 'CC BY-SA 3.0 Unported',
                'BSD-2-Clause' => 'BSD 2-clause License',
                'BSD-3-Clause' => 'BSD 3-clause License',
                'BSL-1.0' => 'Boost Software License'
            ] ?>
            <?php if(!$_asset_values['cost']) { ?>
                <option value="" selected="">
                    Select...
                </option>
            <?php } ?>
            <?php foreach($licenses as $id => $name) { ?>
                <option value="<?php echo raw($id) ?>" <?php if($id == $_asset_values['cost']) echo 'selected=""'; ?>>
                    <?php echo esc($name) ?>
                </option>
            <?php } ?>
        </select>
        <span class="help-block">The license under which the asset is available. Check <a href="https://opensource.org/licenses">opensource.org</a> for a more detailed description of each. In case an OSI-approved license you are using is missing, you might <a href="https://github.com/godotengine/asset-library/issues">open an issue</a> about it.</span>
        <span class="help-block"><strong>Note:</strong> The license you specify here should also be included in the repository you are submitting under a standard name, such as LICENSE or LICENSE.md</span>
    </div>
</div>

<div class="form-group">
    <label class="col-md-4 control-label required_mark" for="download_provider">Repository host</label>
    <div class="col-md-5">
        <select id="download_provider" name="download_provider" class="form-control" required="">
            <?php if(!$_asset_values['download_provider']) { ?>
                <option value="" selected="">
                    Select...
                </option>
            <?php } ?>
            <?php foreach($constants['download_provider'] as $id => $name) if(is_int($id) && $id >= 0) { ?>
                <option value="<?php echo esc($name) ?>" <?php if($name === $_asset_values['download_provider']) echo 'selected=""'; ?>>
                    <?php echo esc($name) ?>
                </option>
            <?php } ?>
            <?php if((isset($user) && ($user['type'] >= $constants['user_type']['moderator'])) || "Custom" == $_asset_values['download_provider']) { ?>
                <option value="Custom" <?php if("Custom" == $_asset_values['download_provider']) echo 'selected=""'; ?>>
                    Custom (Moderator-only)
                </option>
            <?php } ?>
        </select>
        <span class="help-block">The site or service hosting your repository. If your repository host is missing, you might like to <a href="https://github.com/godotengine/asset-library/issues">open an issue</a> about it.</span>
    </div>
</div>

<div class="form-group">
    <label class="col-md-4 control-label required_mark" for="browse">Repository URL</label>
    <div class="col-md-5">
        <input id="browse" name="browse_url" type="text" placeholder="https://host.example/user/repository" class="form-control input-md" required="" value="<?php echo esc($_asset_values['browse_url']) ?>">
        <span class="help-block">The URL you use to browse your repository.</span>
        <span class="help-block"><strong>Note:</strong> Do not give the clone URL (the one that ends in <code>.git</code>), but give the one you use to browse your code.</span>
        <?php if(isset($user) && ($user['type'] >= $constants['user_type']['moderator'])) { ?>
            <span class="help-block">When using the Custom provider, this is used for browsing only.</span>
        <?php } ?>
    </div>
</div>

<div class="form-group">
    <label class="col-md-4 control-label" for="issues">Issues URL</label>
    <div class="col-md-5">
        <input id="issues" name="issues_url" type="text" placeholder="https://host.example/user/repository/issues" class="form-control input-md" value="<?php echo esc($_asset_values['issues_url']) ?>">
        <span class="help-block">Optional, in case you are not using the one supplied by the repository host.</span>
    </div>
</div>

<div class="form-group">
    <label class="col-md-4 control-label required_mark" for="godot_version">Godot version</label>
    <div class="col-md-5">
        <select id="godot_version" name="godot_version" class="form-control" required="">
            <?php if(!$_asset_values['godot_version']) { ?>
                <option value="" selected="">
                    Select...
                </option>
            <?php } elseif(!in_array($_asset_values['godot_version'], $constants['common_godot_versions'])) { ?>
                <option value="<?php echo esc($_asset_values['godot_version']) ?>" selected="">
                    <?php echo esc(ucfirst($_asset_values['godot_version'])) ?>
                </option>
            <?php } ?>
            <?php foreach($constants['common_godot_versions'] as $version) { ?>
                <option value="<?php echo esc($version) ?>" <?php if($version == $_asset_values['godot_version']) echo 'selected=""'; ?>>
                    <?php echo esc(ucfirst(str_replace('_', ' ', $version))) ?>
                </option>
            <?php } ?>
        </select>
        <!-- <span class="help-block">The version of Godot the asset works with.</span> -->
    </div>
</div>

<div class="form-group">
    <label class="col-md-4 control-label required_mark" for="version">Asset Version</label>
    <div class="col-md-5">
        <input id="version" name="version_string" type="text" placeholder="e.g. 1.0.0 or 1.0" class="form-control input-md" required="" value="<?php echo esc($_asset_values['version_string']) ?>">
        <!-- <span class="help-block">A human-readable version of the asset, e.g. <code>1.0</code> or <code>1.0.0</code>.</span> -->
    </div>
</div>

<div class="form-group">
    <label class="col-md-4 control-label required_mark" for="commit">Download Commit</label>
    <div class="col-md-5">
        <input id="commit" name="download_commit" type="text" placeholder="Commit hash" class="form-control input-md" required="" value="<?php echo esc($_asset_values['download_commit']) ?>" <?php if(isset($user) && ($user['type'] < $constants['user_type']['moderator'])) { ?> pattern="[a-f0-9]{40}([a-f0-9]{24})?" <?php } ?> >
        <span class="help-block">The commit hash that should be downloaded. Expects 40 or 64 hexadecimal digits fully specifying a git commit.</span>
        <?php if(isset($user) && ($user['type'] >= $constants['user_type']['moderator'])) { ?>
            <span class="help-block">When using the Custom provider, this is the download URL.</span>
        <?php } ?>
    </div>
</div>

<div class="form-group">
    <label class="col-md-4 control-label required_mark" for="icon">Icon URL</label>
    <div class="col-md-5">
        <input id="icon" name="icon_url" type="text" placeholder="https://host.example/user/repository/raw/icon.(png|jpg)" class="form-control input-md" required="" value="<?php echo esc($_asset_values['icon_url']) ?>">
    </div>
</div>

<?php for ($i=0; $i < count($_asset_values['previews']) + 3; $i++) {
    if($i < count($_asset_values['previews'])) $preview = $_asset_values['previews'][$i];
    else $preview = ['type' => 'image', 'link' => '', 'thumbnail' => ''];
    ?>
    <div class="panel panel-default">
        <div class="panel-heading form-inline">
            <div class="text-center">
                <span class="panel-title">
                    <?php if($i < count($_asset_values['previews'])) {
                        if(isset($preview['edit_preview_id'])) {
                            echo 'Revise ' . raw($preview['operation']) . ' operation';
                        } else {
                            echo 'Update original preview';
                        }
                    } else {
                        echo 'Add preview';
                    } ?>
                </span>

                <?php if($i < count($_asset_values['previews'])) { ?>
                    <?php if(isset($preview['edit_preview_id'])) { // Existing edit ?>

                        <input type="hidden" name="previews[<?php echo $i ?>][edit_preview_id]" value="<?php echo esc($preview['edit_preview_id']) ?>">
                        <input type="hidden" name="previews[<?php echo $i ?>][enabled]" value="true">
                        <label>
                            <input type="checkbox" name="previews[<?php echo $i ?>][remove]" value="true">
                            Cancel
                        </label>

                    <?php } else { // From base asset ?>
                        <input type="hidden" name="previews[<?php echo $i ?>][preview_id]" value="<?php echo esc($preview['preview_id']) ?>">
                        <label>
                            <input type="checkbox" name="previews[<?php echo $i ?>][enabled]" value="true">
                            Enable
                        </label>
                        <label>
                            <input type="radio" name="previews[<?php echo $i ?>][operation]" value="update" checked="">
                            Update
                        </label>
                        <label>
                            <input type="radio" name="previews[<?php echo $i ?>][operation]" value="remove">
                            Remove
                        </label>

                    <?php } ?>
                <?php } else { ?>
                    <input type="hidden" name="previews[<?php echo $i ?>][operation]" value="insert">
                    <label>
                        <input type="checkbox" name="previews[<?php echo $i ?>][enabled]" value="true" onchange="document.getElementById('previews[<?php echo $i ?>][link]').required = this.checked;">
                        Enable
                    </label>
                <?php } ?>
            </div>
        </div>

        <div class="panel-body">
            <div class="form-group">
                <label class="col-md-4 control-label required_mark" for="previews[<?php echo $i ?>][type]">Type</label>
                <div class="col-md-5">
                    <select id="previews[<?php echo $i ?>][type]" name="previews[<?php echo $i ?>][type]" class="form-control">
                        <?php $types = ['image' => 'Image', 'video' => 'Video'] ?>
                        <?php foreach($types as $id => $name) { ?>
                            <option value="<?php echo raw($id) ?>" <?php if($id == $preview['type']) echo 'selected=""'; ?>>
                                <?php echo esc($name) ?>
                            </option>
                        <?php } ?>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-4 control-label required_mark" for="previews[<?php echo $i ?>][link]">Image/YouTube Link</label>
                <div class="col-md-5">
                    <input id="previews[<?php echo $i ?>][link]" name="previews[<?php echo $i ?>][link]" type="text" placeholder="Image/YouTube Link" class="form-control input-md" value="<?php echo esc($preview['link']) ?>">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-4 control-label" for="previews[<?php echo $i ?>][thumbnail]">Link to thumbnail</label>
                <div class="col-md-5">
                    <input id="previews[<?php echo $i ?>][thumbnail]" name="previews[<?php echo $i ?>][thumbnail]" type="text" placeholder="Image Link" class="form-control input-md" value="<?php echo esc($preview['thumbnail']) ?>">
                </div>
            </div>

        </div>
    </div>
<?php } ?>
